﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace XBoshi.SecurityCommon
{
   [XmlRoot("LicenseClass")]
   public class LicenseClass
    {
        [XmlAttribute(AttributeName = "uid")]
        public string uid { get; set; }
       
        [XmlAttribute(AttributeName = "machinCode")]
        public  string machinCode { get; set; }
        [XmlAttribute(AttributeName = "isTempLincese")]
        public  bool  isTempLincese { get; set; }
        [XmlAttribute(AttributeName = "usingtimes")]
        public long usingtimes { get; set; }
        [XmlAttribute(AttributeName = "validDate")]
        public DateTime  validDate { get; set; }

       //剩余次数
        public long remainTimes { get; set; }

       public override string ToString()
       {
           return base.ToString();
       }

       public String GetXmlSerializer() {
           XmlSerializer xs = new XmlSerializer(typeof(LicenseClass));
           MemoryStream ms=new MemoryStream ();
           xs.Serialize(ms, this);
           StreamReader reader = new StreamReader(ms);
           reader.BaseStream.Seek(0, SeekOrigin.Begin);
           string text = reader.ReadToEnd();
           ms.Close();
           reader.Close();
           return text;
       }

       public LicenseClass ParaseXmlSerializer(String xmlString)
       {
           try
           {
               XmlSerializer xs = new XmlSerializer(typeof(LicenseClass));
               MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(xmlString));

               LicenseClass lc = (LicenseClass)xs.Deserialize(ms);
               ms.Close();
               return lc;
           }catch(Exception ex){
            return null;
           } 
       }


   }
}
